package com.example.mymvvmdemo.models.forget;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityForgetBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.models.login.LoginActivity;
import com.example.mymvvmdemo.models.register.RegisterActivity;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.HashMap;

public class ForgetActivity extends BaseLoadActivity<ActivityForgetBinding> implements View.OnClickListener, IView.PostBodyView {

    public static void startForgetActivity(Activity activity) {
        Intent intent = new Intent(activity, ForgetActivity.class);
        activity.startActivity(intent);
    }

    private ForgetViewModel viewModel;
    private PostBodyPresenter postBodyPresenter;

    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;

    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(ForgetViewModel.class);
        postBodyPresenter = new PostBodyPresenter(this);

    }

    @Override
    protected void initListener() {
        binding.tvNext.setOnClickListener(this);
        binding.ivClearName.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);

        binding.etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearName.setVisibility(charSequence.toString().length() > 0
                        ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_clear_name:
                binding.etAccount.setText("");
                break;
            case R.id.tv_next:
                forgetNext();
                break;
            default:
                break;
        }
    }

    /*********************************自定义方法**********************************************/
    private void forgetNext() {

        String account = binding.etAccount.getText().toString();

        if (StrUtils.isEmpty(account)) {
            toast(R.string.input_account, ToastUtils.TOA_S);
            return;
        }

        if (account.contains("@")) {
            //发送邮箱验证码
            startLoadSendEmail(account);
        } else {
            //发送手机号验证码
            startLoadSendSms(account);
        }

    }

    private void startLoadSendEmail(String account) {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("email", account);
        postBodyPresenter.startLoadPostBody(UrlConstants.sendEmailCode, hashMap, UrlTypeConstants.sendEmailCode);
    }

    public void startLoadSendSms(String account) {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("mobile", account);
        postBodyPresenter.startLoadPostBody(UrlConstants.sendSmsCode, hashMap, UrlTypeConstants.sendSmsCode);
    }


    /************************************数据请求回调***************************************/
    @Override
    public void loadPostBodySuccess(String result, String type,int code) {
        if (type.equals(UrlTypeConstants.sendEmailCode)) {
            loadSendEmailCodeSuccess(result);
        } else if (type.equals(UrlTypeConstants.sendSmsCode)) {
            loadSendSmsCodeSuccess(result);
        }
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.sendEmailCode)) {
            loadSendEmailCodeFail();
        } else if (type.equals(UrlTypeConstants.sendSmsCode)) {
            loadSendSmsCodeFail();
        }
    }

    @Override
    public void loadPostBodyAccountError(int code) {

    }

    @Override
    public void loadPostBodyNetWorkError(String type) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /************************************处理请求后数据***************************************/
    private void loadSendSmsCodeSuccess(String result) {
        dismissDialog();
        String account = binding.etAccount.getText().toString();
        viewModel.startSmsCodeActivity(ForgetActivity.this, account);
    }

    private void loadSendSmsCodeFail() {
        dismissDialog();
        String account = binding.etAccount.getText().toString();
        viewModel.startSmsCodeActivity(ForgetActivity.this, account);
    }

    private void loadSendEmailCodeSuccess(String result) {
        dismissDialog();
        String account = binding.etAccount.getText().toString();
        viewModel.startSmsCodeActivity(ForgetActivity.this, account);
    }

    private void loadSendEmailCodeFail() {
        dismissDialog();
        String account = binding.etAccount.getText().toString();
        viewModel.startSmsCodeActivity(ForgetActivity.this, account);
    }


}